﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SelfService.BL.Interface;
using Wicresoft.SelfService.BL;
using System.Configuration;
using Wicresoft.SelfService.DAO;

namespace Wicresoft.SelfService.Portal.Controllers
{
    public class KnowledgeController : BaseController
    {
        //
        // GET: /Konwledge/

        private IKnowledge KnowledgeService;
        private string ARServer;
        private string ARUid;
        private string ARPwd;
        private string Mid_Base;

        public KnowledgeController()
        {
            KnowledgeService = new KnowledgeService(new UserSync());
            ARServer = ConfigurationManager.AppSettings["ARServer"];
            ARUid = ConfigurationManager.AppSettings["ARUid"];
            ARPwd = ConfigurationManager.AppSettings["ARPwd"];
            Mid_Base = ConfigurationManager.AppSettings["ITSM_Mid_Tier"];//without end "/"
        }

        [Authorize]
        public ActionResult Index()
        {
            var RKMs = KnowledgeService.GetTopReferenceRKMRecords(User.Identity.Name,
                ARServer,
                ARUid,
                ARPwd
                );

            foreach (var rkm in RKMs)
            {
                rkm.BaseUrl = string.Format("{0}/arsys/forms/{1}/RKM%3AKnowledgeArticleManager/Display+View/?eid={2}",Mid_Base ,ARServer,rkm.RKMId);
                if (string.IsNullOrEmpty(rkm.ArticleTitle))
                    rkm.ArticleTitle = "<无标题>";
            }

            RKMModel model = new RKMModel();
            model.RKMs = RKMs;
            model.SearchBaseUrl = string.Format("{0}/arsys/forms/{1}/RKM%3ASearchDialog/Default+Admin+View/?format=html&F302258625=", Mid_Base, ARServer);

            return View(model);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            KnowledgeService.Dispose();
        }

    }
}
